{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--BOOK_INFORMATION-->\n",
    "<img align=\"left\" style=\"padding-right:10px;\" src=\"figures/PHydro-cover-small.png\">\n",
    "*This is the Jupyter notebook version of the [Python in Hydrology](http://www.greenteapress.com/pythonhydro/pythonhydro.html) by Sat Kumar Tomer.*\n",
    "*Source code is available at [code.google.com](https://code.google.com/archive/p/python-in-hydrology/source).*\n",
    "\n",
    "*The book is available under the [GNU Free Documentation License](http://www.gnu.org/copyleft/fdl.html). If you have comments, corrections or suggestions, please send email to satkumartomer@gmail.com.*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--NAVIGATION-->\n",
    "< [9.Numerical Modelling](09.00-Numerical-Modelling.ipynb) | [Contents](Index.ipynb) | [ODE](09.02-ODE.ipynb) >"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9.1 积分\n",
    "\n",
    "假设我们想要对某些函数积分，并且该函数不能够积分解析，于是我们采用数值积分。要检查是否我们的数值积分方案工作正常与否，我们对分析方法有效的函数积分，然后对比我们的解决方案。所以让我们从一个变量函数开始。假设，我们有函数$f(x)=x$来实现0到10范围的插值。我们从数学上指导答案为50。让我们现在尝试数值方法有关的解决方案。我们将使用`scipy`的`integrate`库。对于简单的函数我们可以使用`lambad`函数代替`def`来定义函数。函数`integrate.quad`执行我们的任务。它返回返回积分和积分中的误差。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "50.0 5.551115123125783e-13\n"
     ]
    }
   ],
   "source": [
    "from scipy import integrate\n",
    "y_fun = lambda x:x\n",
    "y,err = integrate.quad(y_fun,0,10)\n",
    "print(y,err)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们得到了的答案为50，这个答案是精确地解析解，并且它说明这种解法的误差非常低。它可能是偶尔间，我们从数值方法得到精确的方案。所以让我们尝试更多的函数，这次是指数。我们将在0到$\\infty$间实现对$f(x)=exp(-x)$的积分。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1.0000000000000002, 5.842606996763696e-11)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "y_func = lambda x:np.exp(-x)\n",
    "y = integrate.quad(y_func,0,np.inf)\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们看到这个解非常接近于解析解(1.0)。这些函数仅有变量作为输入，但是我们可能想要额外的参数在函数中，例如，$f(x)=exp(-ax)$,并为这个例子分配`a=0.5`。`integrate.quad`也提供了以额外参数为条件的选项。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.786938680574733"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f = lambda x,a:np.exp(-a*x)\n",
    "y,err = integrate.quad(f,0,1,args=(0.5,))\n",
    "y"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
